﻿using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using StartsEAS_API.BLL.Interfaces.IWorkerB;
using StartsEAS_API.DAL.WorkerD;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StartsEAS_API.BLL.WorkerB
{
    public class WorkerBLL : IWorkerBLL
    {

        private readonly StarsEASContext _context;

        public WorkerDAL _workerDal;

        public WorkerBLL(IServiceScopeFactory context)
        {
            _context = context.CreateScope().ServiceProvider.GetRequiredService<StarsEASContext>();
            _workerDal = new(_context);
        }
        public ResultUtils GetAllWorker()
        {
            return _workerDal.GetAllWorker();
        }

        public ResultUtils GetWorder(string workerId)
        {
            Worker worker = _workerDal.GetWorker(workerId);
            if (worker == null)
                return new ResultUtils()
                {
                    code = 500,
                    message = "查无该教职员工"
                };
            return new ResultUtils()
            {
                code = 200,
                message = worker
            };
        }

        public ResultUtils InsertWorker(JObject data) {
            string jsonStr = JsonConvert.SerializeObject(data);
            Worker wor = JsonConvert.DeserializeObject<Worker>(jsonStr);    
            return _workerDal.InsertWorker(wor);
        }

        public ResultUtils UpdateWorker(JObject data)
        {
            string jsonStr = JsonConvert.SerializeObject(data);
            Worker worker = JsonConvert.DeserializeObject<Worker>(jsonStr);
            return _workerDal.UpdateWorker(worker);
        }
    }
}
